<template>
  <div class="settings-container">
    <!-- 使用 Loading 组件 -->
    <Loading v-if="isLoading" />

    <!-- 页面标题 -->
    <div class="page-header">
      <h1 class="page-title">设置</h1>
    </div>

    <!-- 角色选择卡片 -->
    <div class="settings-card">
      <div class="card-header">
        <div class="card-icon">👤</div>
        <div>
          <h2 class="card-title">角色选择</h2>
          <p class="card-desc">选择您的角色以获得个性化主题</p>
        </div>
      </div>

      <div class="role-selector">
        <div
            class="role-option"
            :class="{ active: currentRole === '1' }"
            @click="changeRole('1')"
        >
          <div class="role-icon-wrapper">
            <div class="role-icon">👨</div>
          </div>
          <div class="role-name">男性</div>
          <div class="role-check" v-if="currentRole === '1'">✓</div>
        </div>

        <div
            class="role-option"
            :class="{ active: currentRole === '2' }"
            @click="changeRole('2')"
        >
          <div class="role-icon-wrapper">
            <div class="role-icon">👩</div>
          </div>
          <div class="role-name">女性</div>
          <div class="role-check" v-if="currentRole === '2'">✓</div>
        </div>

        <div
            class="role-option"
            :class="{ active: currentRole === '99' }"
            @click="changeRole('99')"
        >
          <div class="role-icon-wrapper">
            <div class="role-icon">👥</div>
          </div>
          <div class="role-name">通用</div>
          <div class="role-check" v-if="currentRole === '99'">✓</div>
        </div>
      </div>
    </div>

    <!-- 主题色卡片 -->
    <div class="settings-card">
      <div class="card-header">
        <div class="card-icon">🎨</div>
        <div>
          <h2 class="card-title">当前主题</h2>
          <p class="card-desc">您的主题色将应用于整个应用</p>
        </div>
      </div>

      <div class="theme-display">
        <div class="theme-color-item">
          <div class="color-preview primary-first"></div>
          <span class="color-label">主色调</span>
        </div>
        <div class="theme-color-item">
          <div class="color-preview primary-second"></div>
          <span class="color-label">辅助色</span>
        </div>
        <div class="theme-gradient-preview">
          <div class="gradient-bar"></div>
          <span class="color-label">渐变效果</span>
        </div>
      </div>
    </div>

  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import Loading from "../../components/Loading/Loading.vue";

const router = useRouter();
const currentRole = ref('');
const isLoading = ref(false);

// 初始化当前角色
onMounted(() => {
  const userId = localStorage.getItem('userId');
  currentRole.value = userId || '1';
});

// 切换角色并应用主题
const changeRole = (roleId) => {
  // 如果角色已经是选中状态，不做任何操作
  if (currentRole.value === roleId) return;

  // 显示加载中
  isLoading.value = true;

  // 更新当前角色
  currentRole.value = roleId;

  // 更新 localStorage
  localStorage.setItem('userId', roleId);

  // 应用主题
  applyTheme();

  // 模拟异步过程，0.8秒后隐藏加载状态
  setTimeout(() => {
    isLoading.value = false;
  }, 800);
};

// 应用主题的函数 (与 App.vue 中的逻辑保持一致)
const applyTheme = () => {
  const userId = localStorage.getItem('userId');
  if (userId === '1') {
    document.body.classList.remove('theme-female');
    document.body.classList.add('theme-male');
  } else if (userId === '2') {
    document.body.classList.remove('theme-male');
    document.body.classList.add('theme-female');
  } else {
    // 对于 all 角色 (userId = 99)，移除所有主题类
    document.body.classList.remove('theme-male', 'theme-female');
  }
};
</script>

<style scoped>
.settings-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

/* 页面标题 */
.page-header {
  text-align: left;
  margin-bottom: 8px;
}

.page-title {
  font-size: 32px;
  font-weight: 700;
  margin: 0 0 8px 0;
  background: var(--primary-gradient);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.page-subtitle {
  margin: 0;
  color: #666;
  font-size: 14px;
}

/* 卡片样式 */
.settings-card {
  background: #fff;
  border-radius: 16px;
  padding: 24px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  transition: transform 0.2s, box-shadow 0.2s;
}

.settings-card:hover {
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.12);
}

/* 卡片头部 */
.card-header {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  margin-bottom: 20px;
}

.card-icon {
  font-size: 28px;
  width: 48px;
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  border-radius: 12px;
  flex-shrink: 0;
}

.card-title {
  font-size: 18px;
  font-weight: 600;
  margin: 0 0 4px 0;
  color: var(--text-color, #333);
}

.card-desc {
  margin: 0;
  color: #999;
  font-size: 13px;
  line-height: 1.4;
}

/* 角色选择器样式 */
.role-selector {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
}

.role-option {
  position: relative;
  aspect-ratio: 1;
  border-radius: 16px;
  background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  border: 3px solid transparent;
  overflow: hidden;
}

.role-option::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--primary-gradient);
  opacity: 0;
  transition: opacity 0.3s;
  z-index: 0;
}

.role-option:hover {
  transform: translateY(-6px) scale(1.02);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15);
}

.role-option.active {
  border-color: var(--primary-color-first, #3498DB);
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.9) 0%, rgba(255, 255, 255, 0.7) 100%);
  box-shadow: 0 4px 16px rgba(var(--primary-color-first-rgb, 52, 152, 219), 0.3);
}

.role-option.active::before {
  opacity: 0.1;
}

.role-icon-wrapper {
  position: relative;
  z-index: 1;
  width: 64px;
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: white;
  border-radius: 50%;
  margin-bottom: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s;
}

.role-option:hover .role-icon-wrapper {
  transform: scale(1.1);
}

.role-icon {
  font-size: 36px;
}

.role-name {
  position: relative;
  z-index: 1;
  font-size: 15px;
  font-weight: 600;
  color: var(--text-color, #333);
  margin-top: 4px;
}

.role-check {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 24px;
  height: 24px;
  background: var(--primary-color-first, #3498DB);
  color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: bold;
  z-index: 2;
  animation: checkIn 0.3s ease-out;
}

@keyframes checkIn {
  0% {
    transform: scale(0);
    opacity: 0;
  }
  50% {
    transform: scale(1.2);
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}

/* 主题色展示 */
.theme-display {
  display: flex;
  gap: 20px;
  flex-wrap: wrap;
}

.theme-color-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.color-preview {
  width: 72px;
  height: 72px;
  border-radius: 16px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  transition: transform 0.3s;
  cursor: pointer;
}

.color-preview:hover {
  transform: scale(1.1);
}

.primary-first {
  background: var(--primary-color-first, #3498DB);
}

.primary-second {
  background: var(--primary-color-second, #9FACE6);
}

.theme-gradient-preview {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  flex: 1;
  min-width: 150px;
}

.gradient-bar {
  width: 100%;
  height: 72px;
  background: var(--primary-gradient);
  border-radius: 16px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  transition: transform 0.3s;
  cursor: pointer;
}

.gradient-bar:hover {
  transform: scale(1.05);
}

.color-label {
  font-size: 12px;
  color: #666;
  font-weight: 500;
}

/* 信息卡片 */
.info-card {
  background: linear-gradient(135deg, #FFF9E6 0%, #FFE5B4 100%);
  border: 2px solid #FFD700;
}

.info-content {
  display: flex;
  align-items: center;
  gap: 12px;
}

.info-icon {
  font-size: 32px;
  flex-shrink: 0;
}

.info-text {
  margin: 0;
  color: #856404;
  font-size: 14px;
  line-height: 1.6;
}

/* 响应式调整 */
@media (max-width: 600px) {
  .settings-container {
    padding: 15px;
  }

  .page-title {
    font-size: 28px;
  }

  .role-selector {
    grid-template-columns: repeat(3, 1fr);
    gap: 12px;
  }

  .role-icon-wrapper {
    width: 48px;
    height: 48px;
  }

  .role-icon {
    font-size: 28px;
  }

  .role-name {
    font-size: 13px;
  }

  .theme-display {
    gap: 12px;
  }

  .color-preview {
    width: 60px;
    height: 60px;
  }

  .gradient-bar {
    height: 60px;
  }

  .card-header {
    gap: 10px;
  }

  .card-icon {
    width: 40px;
    height: 40px;
    font-size: 24px;
  }
}

@media (max-width: 400px) {
  .role-selector {
    gap: 8px;
  }

  .role-icon-wrapper {
    width: 40px;
    height: 40px;
  }

  .role-icon {
    font-size: 24px;
  }

  .role-name {
    font-size: 12px;
  }
}
</style>